package org.demo.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class HelpFrame extends BaseFrame{

	private static final long serialVersionUID = 2554990354776193684L;
	
	private final static String HELP = "<html>"
			+ "<body>"
			+ "Please refer to README.txt"
			+ "<br>"
			+ "<br>"
			+ "You can click \"Yes\" to open the file directly"
			+ "</body>"
			+ "</html>"; 

	public HelpFrame() {
		super();
		setTitle(TITLE_HELP);
		setSize(320, 240);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}

	@Override
	protected void initLayout() {
		setLayout(null);
		JLabel help = new JLabel(HELP);
		help.setBounds(20, 40, 280, 60);
		add(help);
		JButton yes = new JButton("Yes");
		yes.setBounds(40, 130, 110, 30);
		yes.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				openFile();
				dispose();
			}
		});
		add(yes);
		JButton no = new JButton("No");
		no.setBounds(170, 130, 110, 30);
		no.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		add(no);
	}
	
	private void openFile() {
		File file = new File("README.txt");
		if (!file.exists()) {
			JOptionPane.showMessageDialog(null, "README.txt is no exist", "Result", JOptionPane.ERROR_MESSAGE);
			return;
		}
		try {
			Runtime.getRuntime().exec("notepad " + file.getAbsolutePath());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
